package zhtt.jasperreports02.controller;

import com.mysql.jdbc.JDBC4ResultSet;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhtt.jasperreports02.db.MysqlUtils;
import zhtt.jasperreports02.entity.JavaBeanPerson;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.SQLException;


@Controller
public class Demo01Controller {

    @Autowired
    private HttpServletRequest request;

    /**
     * 返回iReport报表视图
     * @param model  * @return
     */
    @RequestMapping(value = "demo02/report", method = RequestMethod.GET)
    public String report(Model model) {
        // 报表数据源
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList(), false);
        // 动态指定报表模板url
        model.addAttribute("url", "/WEB-INF/classes/reports/report1.jasper");
        model.addAttribute("format", "pdf"); // 报表格式
        model.addAttribute("jrMainDataSource", jrDataSource);
        return "iReportView"; // 对应jasper-defs.xml中的bean id
    }

    @RequestMapping(value = "demo02/report03", method = RequestMethod.GET)
    public String report03(Model model) {
        // 报表数据源
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
        // 动态指定报表模板url
        model.addAttribute("url", "/WEB-INF/classes/reports/report3.jasper");
        model.addAttribute("format", "pdf"); // 报表格式
        model.addAttribute("jrMainDataSource", jrDataSource);
        return "iReportView"; // 对应jasper-defs.xml中的bean id
    }

    @RequestMapping(value = "demo02/report04", method = RequestMethod.GET)
    public String report04(Model model) {
        // 报表数据源
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
        // 动态指定报表模板url
        model.addAttribute("url", "/WEB-INF/classes/reports/report4.jasper");
        model.addAttribute("format", "pdf"); // 报表格式
        model.addAttribute("jrMainDataSource", jrDataSource);
        return "iReportView"; // 对应jasper-defs.xml中的bean id
    }
}
